﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 原神曲目生成器
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btn_read_Click(object sender, EventArgs e)
        {
            //打开曲目曲谱
            OpenFileDialog dialog = new OpenFileDialog();
            //判断保存的路径是否存在
            if (!Directory.Exists(Application.StartupPath + @"\Music"))
            {
                //创建路径
                Directory.CreateDirectory(Application.StartupPath + @"\Music");
            }
            //设置默认打开路径(项目安装路径+\Music)
            dialog.InitialDirectory = Application.StartupPath + @"\Music";
            //设置打开标题、后缀
            dialog.Title = "请选择导入txt文件";
            dialog.Filter = "txt文件|*.txt";
            string path = "";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                //得到打开的文件路径（包括文件名）
                path = dialog.FileName.ToString();
                
                try
                {
                    // 创建一个 StreamReader 的实例来读取文件 
                    // using 语句也能关闭 StreamReader
                    using (StreamReader sr = new StreamReader(path))
                    {
                        List<string> lines = new List<string>();
                        string line;
                        do
                        {
                            line = sr.ReadLine();
                            lines.Add(line);
                        } while (line != null);
                        string dc = ASystem.Decode(lines);
                        File.WriteAllText(path.Replace(".txt", ".ys"), dc);
                      
                    }
                }
                catch (Exception ex)
                {
                    //return "";
                    //throw new Exception(ex.Message);
                }
            }
        }


    

        private void MainForm_Load(object sender, EventArgs e)
        {
            //加载歌曲
            cmb_music_list.Items.Clear();
            //获取当前程序目录，获取所有xml文件绝对地址
            var files = Directory
              .GetFiles(Directory.GetCurrentDirectory()+"\\Music", "*.ys");
            //提取路径地址+/为了在后面把完整路径剔除
            string pathstr = Directory.GetCurrentDirectory() + "\\Music\\";

            List<music> list = new List<music>();
            foreach (var file in files)
            {
                list.Add(new music() { Name = file.ToString().Replace(pathstr, ""), Value = file });               
            }
            cmb_music_list.DataSource = list;
            cmb_music_list.ValueMember = "Value";
            cmb_music_list.DisplayMember = "Name";

        }
       
        //声明常量：(释义可参见windows API)

        const int WS_EX_NOACTIVATE = 0x08000000;

        //重载Form的CreateParams属性，添加不获取焦点属性值。

        protected override CreateParams CreateParams

        {

            get
            {

                CreateParams cp = base.CreateParams;
                cp.ExStyle |= WS_EX_NOACTIVATE;
                return cp;
            }

        }
        Player Players = new Player();
        private void btn_play_Click(object sender, EventArgs e)
        {
            if (Players.IsPlay)
            {
                Players.Stop();
                btn_play.Text = "播放";
            }
            else
            {
                Players.Play(((music)cmb_music_list.SelectedItem).Value);
                btn_play.Text = "停止";
            }
        }
    }
}
